# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time
  protect_from_forgery # See ActionController::RequestForgeryProtection for details

  before_filter :set_i18n
  # Scrub sensitive parameters from your log
  filter_parameter_logging :password
  
  helper_method :current_user  

  layout proc { |controller| controller.request.subdomains.first }
  
  before_filter :set_cache_directory
  
  rescue_from 'CanCan::AccessDenied' do
    session['login_back_url'] = self.request.path
    redirect_to login_path
  end

protected

  def set_i18n
    I18n.locale = 'ar'
  end
  
  def current_user_session  
    return @current_user_session if defined?(@current_user_session)  
    @current_user_session = UserSession.find  
  end  
   
  def current_user  
    @current_user = current_user_session && current_user_session.record  
  end

  def set_cache_directory
    ActionController::Base.page_cache_directory = File.join(Rails.public_path,
      request.subdomains.first)
  end
end
